package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.buz.hjcdriver.bean.AResultBean
import com.buz.hjcdriver.bean.CountryListResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.ToastUtils
import com.lzy.okgo.model.Response
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_changephone.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.fragments.ShowCountryFragment
import lium.lbs.lbsim.utils.DesUtils
import java.util.HashMap
import java.util.concurrent.TimeUnit

class ChangePhoneActivity: BaseActivity() {

    override fun getLayoutId(): Int { return R.layout.activity_changephone  }
    var countryBean: CountryListResultBean?=null
    var isNewPhone=false//设置新手机号
    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("")
        intent?.let {
            if(intent.hasExtra("isNewPhone")){
                isNewPhone = intent.getBooleanExtra("isNewPhone",false)
                login_phone.isEnabled = true
                login_phone.hint = "请输入手机号"
            }
        }
        if(!isNewPhone){
            change_title.text = "解绑手机号"
            val phone = SpUtils.getInstance().get(Constants.phone, "") as String
            login_phone.setText(phone+"")
            login_phone.isEnabled = false
        }
        addOnClickListeners(R.id.login_getsms,R.id.btn_start_login,R.id.showCountybtn)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v!!.id){
            R.id.login_getsms ->{
                getSMS()
            }
            R.id.btn_start_login -> {
                if (isNewPhone){
                    setNewPhone()
                }else{
                    checkSMSCode()
                }
            }
            R.id.showCountybtn -> {
                showCounty()
            }
        }
    }

    private fun setNewPhone() {
        if(TextUtils.isEmpty(login_phone.text.toString()) ||
            login_phone.text.toString().length!=11){
            ToastUtils.showToast("手机号码无效")
            return
        }
        if(TextUtils.isEmpty(login_sms.text.toString()) ||
            login_sms.text.toString().length!=6){
            ToastUtils.showToast("验证码无效")
            return
        }
        val param = HashMap<String, String>()
        param["phone"] = login_phone.text.toString()
        param["code"] = login_sms.text.toString()
        param["countryCode"] = "86"
        countryBean?.let {
            param["countryCode"] = countryBean!!.code
        }
        postData("/member/resetPhone",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg+"")
                finish()
            }
        })
    }

    private fun checkSMSCode() {
        if(TextUtils.isEmpty(login_phone.text.toString()) ||
            login_phone.text.toString().length!=11){
            ToastUtils.showToast("手机号码无效")
            return
        }
        if(TextUtils.isEmpty(login_sms.text.toString()) ||
            login_sms.text.toString().length!=6){
            ToastUtils.showToast("验证码无效")
            return
        }
        val param = HashMap<String, String>()
        param["phone"] = login_phone.text.toString()
        param["code"] = login_sms.text.toString()
        param["countryCode"] = "86"
        countryBean?.let {
            param["countryCode"] = countryBean!!.code
        }
        postData("/member/memberCheckCode",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg+"")
                val intent = Intent(this@ChangePhoneActivity, ChangePhoneActivity::class.java)
                intent.putExtra("isNewPhone",true)
                startActivity(intent)
                finish()
            }
        })
    }




    private fun showCounty() {
        var showCountryFragment = ShowCountryFragment()
        showCountryFragment
        showCountryFragment.show(supportFragmentManager,"showCountryFragment")
    }

    private fun getSMS() {
        if(TextUtils.isEmpty(login_phone.text.toString()) ||
            login_phone.text.toString().length!=11){
            ToastUtils.showToast("手机号码无效")
            return
        }

        val param = HashMap<String, String>()
        val encryptedString = DesUtils.encryptDESwithBase64(login_phone.text.toString(), "Chat2019")
        param["phone"] = encryptedString

        if(isNewPhone){
            param["type"] = "register"
        }else{
            param["type"] = "forget"
        }
        param["countryCode"] = "86"
        countryBean?.let {
            param["countryCode"] = countryBean!!.code
        }

        postData("/member/getIdentifyingCode",param, object : DialogCallback<ResponseBean<AResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                ToastUtils.showToast(response.body().msg+"")
                time(60)
            }
        })

    }

    public fun setConutryBean(countryBean: CountryListResultBean){
        this.countryBean = countryBean
        showCountybtn.text ="+" +countryBean.code
    }

    fun time(time: Long) {
        login_getsms.isEnabled=false
        login_phone.isEnabled=false
        addDisposable(Observable.interval(1, TimeUnit.SECONDS)
            .map { aLong -> time - aLong }
            .take(time + 1)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnComplete {
                login_getsms.isEnabled=true
                login_getsms.text ="获取"
                login_phone.isEnabled=true
            }
            .subscribe {
                login_getsms.text = ""+it.toString()+  "秒";
            })
    }

    override fun getNetWorkData() {
    }
}

